#!/bin/bash
# usage get.sh -c -B BASE -h -P <DESTINATION> -y [ URL_LIST ]
#

function usage
{
  echo "usage: ${0##*/} -b BASE -c -d DESTINATION -r -y URL_LIST" 1>&2
  echo "       Use \`wget' utility to get files from the net." 1>&2
  echo "" 1>&2
  echo "    -b BASE            Prefix all URLs with BASE." 1>&2
  echo "    -c                 Get URL_LIST from cliboard." 1>&2
  echo "    -d DESTINATION     Put downloaded files to DESTINATION." 1>&2
  echo "    -r                 Rename the downloaded files, assuming the names" 1>&2
  echo "                       are in \`showfile@name=114553075562.jpg' style" 1>&2
  echo "    -y                 Enable proxy of wget." 1>&2
  echo "    URL_LIST           List of files containing URLs." 1>&2
  return 1
}

if [ $# -eq 0 ]; then
  usage
  exit
fi

# initialization
# working directory
workdir=~/get
prefix=$workdir/prefix.txt

# list of URL files
url_files=
url_base=
# flags
clipboard=no
proxy=no
rename=no
# output path
destination=

# options parsing
while [ -n "$1" ]; do
  case $1 in
    -b )
      shift
      url_base="$1"
      if [ -z "$url_base" ]; then
        echo "warning: -b: URL base not provided, ignore." 1>&2
      else
        case $url_base in
        *://* )
        ;;
        * )
          echo "warning: -b: $url_base: seems not a proper URL base, ignore." 1>&2
          url_base=""
        ;;
        esac
      fi
    ;;
    -c )
    clipboard=yes
    ;;
    -d )
      shift
      destination=$1
      if [ -z "$destination" ]; then
        echo "warning: -d: no proper value for destination, ignore." 1>&2
      fi
      if [ ! -d "$destination" ] && ! mkdir -p "$destination" ; then
        echo "error: $destination: path invalid. quit..." 1>&2
        exit 1
      fi
    ;;
    -r )
      rename=yes
    ;;
    -y )
      proxy=yes
    ;;
    * )
      url_files=${url_files}" "$1
    ;;
  esac
  shift
done # options parsing

if [ x$clipboard = xyes ]; then
  # use clipboard only
  getclip |
    sed -e 's@http://freecity.cn/@http://www.zju88.cn/@gi' \
      -e 's@http://bbs.freecity.cn/@http://www.zju88.cn/@gi' \
    > $workdir/clip.txt
  url_files=$workdir/clip.txt
fi

# construct `wget' options
wget_options=
if [ x$proxy = xyes ]; then
  wget_options="-Y on"
else
  wget_options="-Y off"
fi

# output path
if [ -n "$destination" ]; then
    :
else
  # generate output dir automatically
  # increase the prefix char
  prechar=$(tr zabcdefgqrstvwx abcdefgqrstvwxz <$prefix)
  # save back the new prefix char
  echo $prechar >$prefix
  # construct destination path
  destination=$workdir/${prechar}_$(date +%Y-%m-%d_%H.%M.%S).d
fi
wget_options="$wget_options -P $destination"

echo "Destination: $destination" 1>&2

# base URL
if [ -n "$url_base" ]; then
  wget_options="$wget_options -B $url_base"
fi

# traverse the files
for f in $url_files; do
  echo "File: $f"
  for url in $(cat $f); do
    final_url=${url_base}${url}
    echo "URL: $final_url" 1>&2

    case $final_url in
    *://* )
      # okay, let's download
      # note: -B is passed to `wget', so use $url only
      echo "wget $wget_options $url" 1>&2
      wget $wget_options $url
    ;;
    * )
    # jesus, not a URL?
    echo "warning: $final_url: format suspicious, ignore." 1>&2
    ;;
    esac
  done
  echo # dummy blank line, for clearness
done

# now see if renaming is needed
if [ x$rename = xyes ] && [ -d $destination ]; then
  pushd .
  cd $destination
  for f in *; do
    # showfile?name=114553075562.jpg
    newfilename=${f/showfile@name=/}
    [ $f != $newfilename ] && mv -f "${f}" "${newfilename}"
  done
  popd
fi

# byebye :P
echo ".DONE - $(date)"
